package jin.competition.jan17;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StuDao {

	public boolean StuRegister(Student stu) {
		BaseDao bd = new BaseDao();
		Connection con = bd.getCon();
		String sql1 = "insert into student (name,stuid) values(?,?)";
		String sql2 = "insert into homework (name,stu_id,mark,havehanded) values(?,?,?,?)";
		// String sql = "SELECT STUDENT.stuid, STUDENT.name, homework.havehanded
		// FROM Student INNER JOIN homework ON student.name=homework.name and
		// homework.havehanded='no'";
		try {
			PreparedStatement ps1 = con.prepareStatement(sql1);
			PreparedStatement ps2 = con.prepareStatement(sql2);
			ps1.setString(1, stu.getName());
			ps1.setInt(2, stu.getStuid());
			ps1.executeUpdate();

			ps2.setString(1, stu.getName());
			ps2.setInt(2, stu.getStuid());
			ps2.setString(3, stu.getMark());
			ps2.setString(4, stu.getHavehanded());

			ps2.executeUpdate();

			if (ps1 != null) {
				ps1.close();
			}
			if (ps2 != null) {
				ps2.close();
			}
			if (con != null) {
				con.close();
			}

		} catch (SQLException e ) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean excellext() {
		BaseDao bd = new BaseDao();
		Connection con = bd.getCon();
		String sql = "SELECT name,havehanded FROM homework where havehanded='false'";
		String sql2 = "SELECT name,havehanded FROM homework where havehanded='false'";
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			// ResultSet goodNum=(ResultSet)rs;
			int num=0;
			while (rs.next()) {
				System.out.println(rs.getString("name") + " " + rs.getString("havehanded"));
				++num;
			
			}
			System.out.println(num);
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (con != null) {
				con.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	public boolean delete(){
		BaseDao bd=new BaseDao();
		Connection con=bd.getCon();
		String sql="delete from homework,student";
		try {
			PreparedStatement ps=con.prepareStatement(sql);
			ps.executeUpdate();
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
		
	}
}
